package com.gitee.zeric.codec.controller;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

@Controller
@RequiredArgsConstructor
@RequestMapping("/codec")
@Api(value = "route", tags = "路由跳转")
public class DpsRouteController {
//    private final DfsSettingsProperties setting;

    @GetMapping("/{name}")
    public void resource(@PathVariable(name = "name") String name, HttpServletResponse response,HttpServletRequest request) throws IOException {
        if (StrUtil.isNotBlank(name)) {
            js(name, FileNameUtil.getSuffix(name), response,request);
        }
    }


    @RequestMapping(value = "/{type}/{name}")
    public void js(@PathVariable(name = "name") String name, @PathVariable(name = "type") String type, HttpServletResponse response,HttpServletRequest request) throws IOException {
//         if(name.equals("dfs.html")){
//             System.out.println(1111);
//             UserLoginInterceptor userLoginInterceptor=new UserLoginInterceptor();
//             try {
//                 final Boolean flag = userLoginInterceptor.preHandle(request, response);
//                 if(!flag){
//                     return;
//                 }
//             } catch (Exception e) {
//                 e.printStackTrace();
//             }
//         }

        InputStream inputStream = null;
        if (type.equals("html") || type.equals("ico")) {
            inputStream = this.getClass().getClassLoader().getResourceAsStream("static/" + name);
        } else if(type.equals("datajson")){
            inputStream = this.getClass().getClassLoader().getResourceAsStream("static/dfs/"+ type + "/" + name);
        }
        else {
            inputStream = this.getClass().getClassLoader().getResourceAsStream("static/" + type + "/" + name);
        }

        byte[] bytes = IoUtil.readBytes(inputStream);
        if (inputStream != null) {
            inputStream.close();
        }
        if (bytes != null) {
            if (name.endsWith(".html")) {
                response.setContentType("text/html; charset=utf-8");
            } else if (name.endsWith(".ico")) {
                response.getOutputStream().write(bytes);
                return;
            } else if (name.endsWith(".css")) {
                response.setContentType("text/css;charset=utf-8");
            } else if (name.endsWith(".js")) {
                response.setContentType("text/javascript;charset=utf-8");
            }
            else if (name.endsWith(".json")) {
                response.setContentType("application/json;charset=utf-8");
            }
            else if (name.endsWith(".png") || name.endsWith(".jpg") || name.endsWith(".svg") || name.endsWith(".ttf") || name.endsWith(".woff") ) {
                response.getOutputStream().write(bytes);
                return;
            }
            String result = new String(bytes, StandardCharsets.UTF_8);
            response.getWriter().write(result);
        }
    }


//    @PostMapping("/dfsapi/login")
//    @ResponseBody
//    public R login(@Validated @RequestBody UserVo userVo, HttpServletResponse response){
//        String username=setting.getUsername();
//        String password=setting.getPassword();
//
//        if(StrUtil.isBlank(username)){
//            username="jfcloud";
//        }
//        if(StrUtil.isBlank(password)){
//            password="jfcloudjfcloud";
//        }
//        if(!(username.equals(userVo.getUsername()) && password.equals(userVo.getPassword()))){
//             return R.failed("用户名或密码错误");
//        }
//        String token = null;
//        try {
//            token = JWTUtils.createToken(userVo.getUsername());
//            response.setHeader(JWTUtils.USER_LOGIN_TOKEN, token);
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
//        return R.ok(token);
//    }



}
